package utils

import (
	"log/slog"
	"os"

	"github.com/vishvananda/netlink"
)

var (
	log = slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{AddSource: true}))
)

func GetInterfaces() ([]string, error) {
	var interfaces []string
	links, _ := netlink.LinkList()
	for _, nic := range links {
		if nic.Type() == "device" && nic.Attrs().EncapType == "ether" {
			name := nic.Attrs().Name
			if nic.Attrs().OperState == netlink.OperUp {
				interfaces = append(interfaces, name)
			} else {
				log.Info("interface is not up, will ignore it", "interface", name)
			}
		}
	}
	return interfaces, nil
}
